<template>
  <el-dialog :title="updateTitle" :visible="dialogVisible" @close="close">
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" placeholder="2-10个字符" style="width:80%;" size="mini" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" placeholder="2-10个字符" style="width:80%;" size="mini" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="formData.managerId" placeholder="请选择负责人" style="width:80%;" size="mini">
          <el-option v-for="item in ManagerList" :key="item.id" :value="item.id" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" type="textarea" placeholder="1-100个字符" style="width:80%;" :rows="4" size="mini" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" size="mini" @click="btnOk">确认</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartment, getManagerList, addDepartment, updateDepartment, getDepartmentDetail } from '@/api/department'
export default {
  name: 'AddDept',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    addId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '', // 负责人id
        introduce: '' // 部门介绍
      },
      ManagerList: [], // 负责人列表
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { trigger: 'blur', validator: async(rule, value, callback) => {
            let res = await getDepartment()
            if (this.formData.id) {
              res = res.filter((item) => item.id !== this.formData.id)
            } else {
              res = await getDepartment()
            }
            if (res.some(item => item.name === value)) {
              callback(new Error('已经有该部门名称了'))
            } else {
              callback()
            }
          } },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
            let res = await getDepartment()
            if (this.formData.id) {
              res = res.filter(item => item.id !== this.formData.id)
            }
            if (res.some(item => item.code === value)) {
              callback(new Error('已有该部门编码了'))
            } else {
              callback()
            }
          } },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'blur' }
        ],
        managerId: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '请输入1-100个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    updateTitle() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    // 关闭弹出框
    close() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '', // 负责人id
        introduce: '' // 部门介绍
      }
      this.$emit('update:dialogVisible', false)
      this.$refs.form.resetFields()
    },
    // 确认按键
    btnOk() {
      this.$refs.form.validate(async isOk => {
        let msg = '新增部门成功'
        if (isOk) {
          if (this.formData.id) {
            msg = '修改部门成功'
            await updateDepartment(this.formData)
          } else {
            await addDepartment({ ...this.formData, pid: this.addId })
          }
          this.$message.success(msg)
          this.close()
          this.$emit('listChange')
        }
      })
    },
    // 获取负责人列表
    async getManagerList() {
      this.ManagerList = await getManagerList()
    },
    // 获取修改部门信息
    async getDepartmentDetail() {
      this.formData = await getDepartmentDetail(this.addId)
    }
  }
}
</script>

<style scoped>
</style>
